import Dialog from '@vant/weapp/dialog/dialog';
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    mainColor: app.globalData.mainColor,
    baseImgUrl: app.globalData.baseImgUrl,
    info: {},
    show: false,
    showImg: "",
    active: 0,
    examine: false
  },

  //获取详情
  getDetail(id) {
    if (id == null) {
      return;
    }
    app.http.get('/gd-fishing/info/detail/' + id).then((res) => {
      // console.log('http-res:', JSON.stringify(res));
      if (app.errorAlert(res)) {
        return;
      }
      let data = res.data;
      this.setData({
        info: data
      })
    })
  },

  //点击显示图片
  clickShowImg(e) {
    this.setData({
      show: true,
      showImg: e.currentTarget.dataset.val
    });
    // console.log("showImg:", this.data.showImg);
  },
  //点击隐藏图片
  clickHideImg() {
    this.setData({
      show: false,
      showImg: ""
    });
  },

  //跳转导航
  go() {
    let that = this;
    wx.openLocation({
      latitude: Number(that.data.info.lat),
      longitude: Number(that.data.info.lng),
      success(res) {
        console.log('success-res:', res);
      },
      fail(res) {
        console.log('fail-res:', res);
      },
    })
  },

  //导航事件
  clickTab(event) {
    if (event.detail == 0) {
      // console.log('关注..')
      this.concern();
    } else if (event.detail == 1) {
      // console.log('点赞..')
      this.praise();
    } else if (event.detail == 2) {
      // console.log('收藏..')
      this.collect();
    } else if (event.detail == 3) {
      // console.log('评论..')
      this.comment();
    }
  },

  //关注
  concern() {
    let that = this;
    app.http.get('/gd-fishing/user/concern/' + that.data.info.userId).then((res) => {
      // console.log('http-res:', JSON.stringify(res));
      if (app.errorAlert(res)) {
        return;
      }
      let val = that.data.info.concern;
      this.setData({
        'info.concern': !val
      })
    })
  },
  //点赞
  praise() {
    let that = this;
    app.http.get('/gd-fishing/info/praise/' + that.data.info.infoId).then((res) => {
      // console.log('http-res:', JSON.stringify(res));
      if (app.errorAlert(res)) {
        return;
      }
      let val = that.data.info.praise;
      this.setData({
        'info.praise': !val
      })
    })
  },
  //收藏
  collect() {
    let that = this;
    app.http.get('/gd-fishing/info/collect/' + that.data.info.infoId).then((res) => {
      // console.log('http-res:', JSON.stringify(res));
      if (app.errorAlert(res)) {
        return;
      }
      let val = that.data.info.collect;
      this.setData({
        'info.collect': !val
      })
    })
  },

  //评论
  comment() {
    app.notOpen();
  },

  //临时开关
  tempSwitch() {
    let that = this;
    app.http.get('/gd-fishing/avoid/other/examine/switch').then((res) => {
      // console.log('http-res:', JSON.stringify(res));
      that.setData({
        examine: res.data,
      })
      // console.log('userInfo:', wx.getStorageSync('userInfo'));
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // console.log("option-id:", options.id)
    // console.log("option-scene:", options.scene)
    //默认小程序内传参
    let id = options.id;
    //如果没有获取到参数，可能是二维码方式进入页面，需要解析"scene"参数
    if (id == null) {
      const scene = decodeURIComponent(options.scene)
      if (scene != null) {
        id = scene.substring(scene.indexOf("=") + 1);
      }
    }
    // console.log("id:", id)
    this.getDetail(id);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.tempSwitch();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})